<?php
namespace app\alipay\controller;

class TradeTestController extends SdkTestController
{
    /**
     * 商家直接创建订单，需要顾客在账单中付款
     */
    public function create()
    {
        $content=[
            "out_trade_no"=>"20190415".time(),
            "buyer_logon_id"=>"bqeuqn3143@sandbox.com",
            "total_amount"=>1.00,
            "subject"=>"trade create test",
        ];
        $request = new \AlipayTradeCreateRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request);
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        }
        else {
            echo "失败";
        }
    }
    /**
     * 商家预创建订单，需要顾客扫描二维码付款
     */
    public function precreate()
    {
        $content=[
            "out_trade_no"=>"20190415".time(),
            "total_amount"=>1.00,
            "subject"=>"trade precreate test",
        ];
        $request = new \AlipayTradePrecreateRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 商户扫描顾客付款码，需要顾客确认付款
     */
    public function pay()
    {
        $code=input("code");//条形码或者二维码的内容 281359729599435276
        if(empty($code)){
            echo "付款码不能为空";
        }
        $content=[
            "out_trade_no"=>"20190415".time(),
            "scene"=>"bar_code",
            "auth_code"=>$code,
            "total_amount"=>1.01,
            "subject"=>"trade pay test",
        ];
        $request = new \AlipayTradePayRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 查询交易状态
     */
    public function query()
    {
        $content=[
            "out_trade_no"=>"201904151555307301",
        ];
        $request = new \AlipayTradeQueryRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 关闭交易
     * 仅顾客未付款时有效
     */
    public function close()
    {
        $content=[
            "out_trade_no"=>"201904151555307301",
        ];
        $request = new \AlipayTradeCloseRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 撤销订单
     * 只有发生支付系统超时或者支付结果未知时可调用撤销
     */
    public function cancel()
    {
        $content=[
            "out_trade_no"=>"201904151555307301",
        ];
        $request = new \AlipayTradeCancelRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 退款
     */
    public function refund()
    {
        $content=[
            "out_trade_no"=>"201904151555307249",
            "refund_amount"=>1.00,
        ];
        $request = new \AlipayTradeRefundRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
    /**
     * 结算
     * 交易成功后进行分账
     */
    public function settle()
    {
        $royalty=new \stdClass();
        $royalty->trans_out="2088102170444333";
        $royalty->trans_in="2088102173187930";
        $royalty->amount=0.1;
        $royalty->desc="royalty test";

        $content=[
            "out_trade_no"=>"201904151555306006",
            "trade_no"=>"2019041522001487931000002987",
            "royalty_parameters"=>[$royalty],
        ];

        $request = new \AlipayTradeOrderSettleRequest();
        $request->setBizContent(json_encode($content));
        $response = $this->client->execute($request); 
        print_r($response);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $response->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }
}
